<script setup lang="js">
import { onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getOrderDetail,deliveryOrder } from "@/api/orderList.js";
import { Tickets } from "@element-plus/icons-vue";
import {ElMessage, ElMessageBox} from "element-plus";

const route = useRoute()
const router = useRouter()
const tableData = ref([]) // 初始化为一个空数组
const companyOptions = ref([
    { label: '顺丰', value: 'SF' },
    { label: '韵达', value: 'YD' },
    { label: '圆通', value: 'YTO' },
    { label: '中通', value: 'ZTO' },
    { label: '申通', value: 'STO' },
])
onMounted(() => {
  getOrderDetail(route.query.id).then(res => {
    if (res.data.data && typeof res.data.data === 'object') {
      // 如果返回的是一个对象，将其放入数组中
      tableData.value = [res.data.data]
    } else {
      console.error('Received data is not an object:', res.data.data);
      tableData.value = [] // 确保 tableData 始终是一个数组
    }
  }).catch(error => {
    console.error('Error fetching order detail:', error);
  })
})
// 格式化地址
const formatAddress = (order) => {
  let str = order.receiverProvince;
  if (order.receiverCity !== null) {
    str += "-" + order.receiverCity;
  }
  str += "-" + order.receiverRegion;
  str += "-" + order.receiverDetailAddress;
  return str;
}

// 取消按钮
const cancel = () => {
  router.push({ path: '/detail/list' })
}

// 确认按钮
const confirm = () => {
  ElMessageBox.confirm('确认发货吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 调用发货接口
    deliveryOrder(tableData.value).then(res => {
      ElMessage.warning(res.data.message)
      ElMessage.success('发货成功')
      router.back()
    }).catch(error => {
      ElMessage.error(error.message)
      ElMessage.error('发货失败，请稍后重试')
      console.error('Error delivering order:', error);
    })
    // 跳转到订单列表
    router.push({ path: '/detail/list' })
  }).catch(() => {
    // 用户取消操作
  })
}
</script>

<template>
  <div class="center-bar">
    <div class="add-btn">
      <el-icon style="font-size: 20px;">
        <Tickets/>
      </el-icon>
      <span>发货列表</span>
    </div>
  </div>
  <div>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="订单编号" align="center">
        <template #default="{ row }">{{ row.orderSn }}</template>
      </el-table-column>
      <el-table-column label="收货人" align="center">
        <template #default="{ row }">
          {{ row.receiverName }}
        </template>
      </el-table-column>
      <el-table-column label="邮政编码" align="center">
        <template #default="{ row }">
          {{ row.receiverPostCode }}
        </template>
      </el-table-column>
      <el-table-column label="收货地址" align="center">
        <template #default="{ row }">
          {{ formatAddress(row) }}
        </template>
      </el-table-column>
      <el-table-column label="配送方式" align="center">
        <template #default="{ row }">
          <el-select
              placeholder="请选择配送方式"
              v-model="row.deliveryCompany">
            <el-option v-for="item in companyOptions"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="物流单号" align="center">
        <template #default="{ row }">
          <el-input v-model="row.deliverySn"></el-input>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="btn-group">
    <el-button @click="cancel">取消</el-button>
    <el-button @click="confirm" type="primary">确定</el-button>
  </div>
</template>

<style scoped lang="scss">
.center-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 20px;
  margin-bottom: 20px;
  border: 1px solid #d5d5d5;
  border-radius: 5px;

  .add-btn {
    display: flex;
    align-items: center;

    span {
      margin-left: 8px;
      font-size: 18px;
    }
  }
}

.btn-group {
  margin-top: 20px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;

  .el-button {
    margin-right: 10px;
  }
}
</style>
